#include "tstring.h"
#include <string>
#include <cctype>

size_t xg_wchar2char( char* dst, size_t dst_size, const WCHAR* src, size_t src_size )
{
	size_t curr_dst_size = 0;

	int cBytes;
	char curr_char[8];

	for( size_t i = 0; i < src_size; i++ )
	{
		wctomb_s(&cBytes, curr_char, 8, src[i]);
		if( !(curr_dst_size + cBytes > dst_size) )
		{
			for( size_t j = 0; (int)j < cBytes; j++ )
			{
				dst[curr_dst_size + j] = curr_char[j];
			}
			curr_dst_size = curr_dst_size + cBytes;
		}
		else
		{
			break;
		}
	}

	return curr_dst_size;
}

size_t xg_char2wchar( WCHAR* dst, size_t dst_size, const char* src, size_t src_size)
{
	size_t curr_dst_size = 0;

	WCHAR curr_char[8];

	for( size_t i = 0; i < src_size; i++ )
	{
		mbtowc( curr_char, &src[i], sizeof(src[i]) );
		if( !(curr_dst_size + sizeof(src[i]) > dst_size) )
		{
			dst[curr_dst_size] = curr_char[0];
			curr_dst_size++;
		}
		else
		{
			break;
		}
	}

	return curr_dst_size;
}

void xg_str2lower( TSTRING& szString )
{
	size_t len = szString.length();
	for(size_t i = 0; i < len; i++)
	{
		szString[i] = std::tolower(szString[i]);
	}
}